#include "wifi_adapter.h"
#include "iot_uart.h"
#include "queue.h"
#include "stdio.h"
#include "string.h"
#include "stdint.h"

/**
 * @brief Enables the station mode.
 *
 * @return Returns {@link WIFI_SUCCESS} if the station mode is enabled; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */

NetModeType g_mode;
extern QueueInfo g_queueInfo[QUEUE_MAX];
uint8_t g_scanQueue = 0xff;
uint8_t g_deviceConfigQueue = 0xff;
uint8_t g_wifiEventQueue = 0xff;
int g_netID = 0;

void ScanCB(WifiErrorCode res)
{
    // 调用扫描通知事件
    if (g_wifiEventQueue != 0Xff)
    {
        QUEUE_NODE *tmp;
        LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_wifiEventQueue].head, QUEUE_NODE, s_list)
        {
            if (((WifiEvent *)(tmp->user_data))->OnWifiScanStateChanged == NULL)
                continue;
            if (res == WIFI_SUCCESS)
                ((WifiEvent *)(tmp->user_data))->OnWifiScanStateChanged(WIFI_STATE_AVAILABLE, g_queueInfo[g_scanQueue].cur_len);
            else
                ((WifiEvent *)(tmp->user_data))->OnWifiScanStateChanged(WIFI_STATE_NOT_AVAILABLE, 0);
        }
    }
}
void ConnectCB(WifiErrorCode res)
{
    // 调用扫描通知事件
    if (g_wifiEventQueue != 0Xff)
    {
        QUEUE_NODE *tmp;
        LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_wifiEventQueue].head, QUEUE_NODE, s_list)
        {
            if (((WifiEvent *)(tmp->user_data))->OnWifiConnectionChanged == NULL)
                continue;
            if (res == WIFI_SUCCESS){
                // 获取linkinfo
                WifiLinkedInfo info;
                GetLinkedInfo(&info);
                ((WifiEvent *)(tmp->user_data))->OnWifiConnectionChanged(WIFI_STATE_AVAILABLE, &info);
            }
            else
                ((WifiEvent *)(tmp->user_data))->OnWifiConnectionChanged(WIFI_STATE_NOT_AVAILABLE, NULL);
        }
    }
}
void DisConnectCB(WifiErrorCode res)
{
    // 调用扫描通知事件
    if (g_wifiEventQueue != 0xff){
        QUEUE_NODE *tmp;
        LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_wifiEventQueue].head, QUEUE_NODE, s_list){
            if (((WifiEvent *)(tmp->user_data))->OnWifiConnectionChanged == NULL)
                continue;
            // 获取linkinfo
            WifiLinkedInfo info;
            GetLinkedInfo(&info);
            if (res == WIFI_SUCCESS)
                ((WifiEvent *)(tmp->user_data))->OnWifiConnectionChanged(WIFI_STATE_NOT_AVAILABLE, NULL);
            else
                ((WifiEvent *)(tmp->user_data))->OnWifiConnectionChanged(WIFI_STATE_AVAILABLE, &info);
        }
    }
}

WifiErrorCode EnableWifi(void)
{
    WifiErrorCode res = WIFI_SUCCESS;

#if WIFI_DEBUG
    printf("======================\n");
    printf("Enable Wi-Fi ...\n");
    // fflush(stdout);
#endif
    g_scanQueue = 0xff;
    char mode = STA;
    res = SendWifiCommand(WIFI_SET_MODE, (char *)(&mode));
    if (res == WIFI_SUCCESS)
    {
        res = WaitWifiResponse(WIFI_SET_MODE, (char *)(&g_mode), WIFI_TIMEOUT);
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif

    return res;
}

/**
 * @brief Disables the station mode.
 *
 * @return Returns {@link WIFI_SUCCESS} if the station mode is disabled; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode DisableWifi(void)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Disable Wi-Fi ... \n");
    // fflush(stdout);
#endif
    // 默认浅睡眠，只要断开连接，就会进入WIFI-OFF状态
    res = Disconnect();
    if (res == WIFI_SUCCESS)
        QueueDestroy(g_scanQueue);
#if WIFI_DEBUG
    PrintWifiError(res);
#endif

    return res;
}

/**
 * @brief Checks whether the station mode is enabled.
 *
 * @return Returns {@link WIFI_STA_ACTIVE} if the station mode is enabled; returns {@link WIFI_STA_NOT_ACTIVE}
 * otherwise.
 * @since 7
 */
int IsWifiActive(void)
{
    WifiErrorCode res;
    // 获取模式
    res = SendWifiCommand(WIFI_GET_MODE, NULL);
    if (res == WIFI_SUCCESS)
    {
        // 等待应答，模式值回传到参数g_mode
        res = WaitWifiResponse(WIFI_GET_MODE, (char *)(&g_mode), WIFI_TIMEOUT);
        if (res == WIFI_SUCCESS)
        {
            // 判断终端功能是否打开
            if (g_mode == STA || g_mode == ATA_AP)
                return WIFI_STA_ACTIVE;
        }
    }
    return WIFI_STA_NOT_ACTIVE;
}

/**
 * @brief Starts a Wi-Fi scan.
 *
 * @return Returns {@link WIFI_SUCCESS} if the Wi-Fi scan is started; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */

WifiErrorCode Scan(void)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Scan wifi ... \n");
    // fflush(stdout);
#endif
    // 不存在，则创建扫描队列
    if (g_scanQueue == 0xff)
    {
        if (QueueCreate(&g_scanQueue, 100, WIFI_SCAN_HOTSPOT_LIMIT))
        {
            printf("%s line %d error : create queue node failed...\n", __func__, __LINE__);
            return ERROR_WIFI_UNKNOWN;
        }
    }
    else
    {
        // 存在，则清空队列
        QueueClear(g_scanQueue);
    }
    res = SendWifiCommand(STA_SCAN_HOTSPOT, NULL);
    if (res == WIFI_SUCCESS)
    {
        res = WaitWifiResponse(STA_SCAN_HOTSPOT, NULL, WIFI_TIMEOUT);
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    // 从队列g_scanQueue解析数据
    ScanCB(res);
    return res;
}

/**
 * @brief Obtains an array of hotspots detected in a Wi-Fi scan.
 *
 * The array of hotspots can be obtained only after the Wi-Fi scan is complete. \n
 *
 * @param result Indicates the array of hotspots detected in a Wi-Fi scan. The array is requested and released by the
 * caller. The value must be greater than or equal to {@link WIFI_SCAN_HOTSPOT_LIMIT}.
 * @param size Indicates the size of the array.
 * @return Returns {@link WIFI_SUCCESS} if the array of hotspots detected in the Wi-Fi scan is obtained; returns an
 * error code defined in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode GetScanInfoList(WifiScanInfo *result, unsigned int *size)
{
    WifiErrorCode res = WIFI_SUCCESS;
    uint8_t i;
    uint32_t len;
    char buf[100];
#if WIFI_DEBUG
    printf("======================\n");
    printf("Get wifi list ... \n");
    // fflush(stdout);
#endif
    // 参数检测
    if (result == NULL || size == NULL)
    {
        res = ERROR_WIFI_INVALID_ARGS;
        return res;
    }
    // 当前队列的长度
    len = g_queueInfo[g_scanQueue].cur_len;
    if (len == 0)
        res = ERROR_WIFI_NOT_AVAILABLE;
    else
    {
        for (i = 0; i < len; i++)
        {
            memset(buf, 0, 100);
            // 出队
            QueueOut(g_scanQueue, buf);
            // 解析
            ParseWifiScanResult(buf, &result[i]);
        }
        *size = len;
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif

    return res;
}

/**
 * @brief Adds a specified hotspot configuration for connecting to a hotspot.
 *
 * This function generates a <b>networkId</b>. \n
 *
 * @param config Indicates the hotspot configuration to add.
 * @param result Indicates the generated <b>networkId</b>. Each <b>networkId</b> matches a hotspot configuration.
 * @return Returns {@link WIFI_SUCCESS} if the specified hotspot configuration is added; returns an error code defined
 * in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode AddDeviceConfig(const WifiDeviceConfig *config, int *result)
{
    WifiErrorCode res = WIFI_SUCCESS;
    // 如果不存在，则创建队列
    if (g_deviceConfigQueue == 0xff)
    {
        if (QueueCreate(&g_deviceConfigQueue, sizeof(WifiDeviceConfig), WIFI_MAX_CONFIG_SIZE))
        {
            return ERROR_WIFI_UNKNOWN;
        }
    }
    // QueueIn(g_deviceConfigQueue, config);
    // 申请节点
    QUEUE_NODE *tmp;
    tmp = (QUEUE_NODE *)malloc(sizeof(QUEUE_NODE));
    if (tmp == NULL)
    {
        return ERROR_WIFI_BUSY;
    }
    // 申请数据域
    tmp->user_data = (char *)malloc(sizeof(WifiDeviceConfig));
    if(tmp->user_data == NULL)
    {
        return ERROR_WIFI_BUSY;
    }
    // 复制数据域
    if (g_queueInfo[g_deviceConfigQueue].cur_len >= g_queueInfo[g_deviceConfigQueue].max_len)
    {
        return ERROR_WIFI_BUSY;
    }
    memcpy(tmp->user_data, config, sizeof(WifiDeviceConfig));
    // 入队
    LOS_ListTailInsert(&g_queueInfo[g_deviceConfigQueue].head, &(tmp->s_list));
    // 生成id
    ((WifiDeviceConfig *)(tmp->user_data))->netId = g_netID;
    *result = g_netID;
    g_netID++;
    // 长度增加
    g_queueInfo[g_deviceConfigQueue].cur_len++;

    return res;
}

/**
 * @brief Obtains all hotspot configurations.
 *
 * Hotspot configurations were added using {@link AddDeviceConfig}. \n
 *
 * @param result Indicates the array of all hotspot configurations. The array is requested and released by the caller.
 * The value must be greater than or equal to {@link WIFI_MAX_CONFIG_SIZE}.
 * @param size Indicates the size of the array.
 * @return Returns {@link WIFI_SUCCESS} if all hotspot configurations are obtained; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode GetDeviceConfigs(WifiDeviceConfig *result, unsigned int *size)
{
    WifiErrorCode res = WIFI_SUCCESS;
    if (NULL == result)
    {
        printf("%s line %d error: bad params result\n", __func__, __LINE__);
        return ERROR_WIFI_INVALID_ARGS;
    }
    // 得到长度
    *size = g_queueInfo[g_deviceConfigQueue].cur_len;
    // 遍历， 不可以出队
    uint16_t i = 0;
    QUEUE_NODE *tmp;
    LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_deviceConfigQueue].head, QUEUE_NODE, s_list)
    {
        memcpy(&result[i++], tmp->user_data, sizeof(WifiDeviceConfig));
    }
    return res;
}

/**
 * @brief Removes a hotspot configuration matching a specified <b>networkId</b>.
 *
 * @param networkId Indicates the <b>networkId</b> matching the hotspot configuration to remove.
 * @return Returns {@link WIFI_SUCCESS} if the hotspot configuration is removed; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode RemoveDevice(int networkId)
{
    WifiErrorCode res = ERROR_WIFI_INVALID_ARGS;
    // 遍历， 不可以出队
    QUEUE_NODE *tmp, *node;
    LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_deviceConfigQueue].head, QUEUE_NODE, s_list)
    {
        if (((WifiDeviceConfig *)(tmp->user_data))->netId == networkId)
        {
            // 找到前驱
            node = LOS_DL_LIST_ENTRY(tmp->s_list.pstPrev, QUEUE_NODE, s_list);
            // 删除
            LOS_ListDelete(&(tmp->s_list));
            // 释放
            free(tmp->user_data);
            free(tmp);
            tmp = node;
            return WIFI_SUCCESS;
        }
    }
    return res;
}

/**
 * @brief Disable a hotspot configuration matching a specified <b>networkId</b>. If the config is disabled, it will
 * not be auto connected.
 *
 * @param networkId Indicates the <b>networkId</b> matching the hotspot configuration to disable.
 * @return Returns {@link WIFI_SUCCESS} if the hotspot configuration is disabled; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode DisableDeviceConfig(int networkId)
{
    WifiErrorCode res = ERROR_WIFI_INVALID_ARGS;
    // 遍历， 不可以出队
    QUEUE_NODE *tmp;
    LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_deviceConfigQueue].head, QUEUE_NODE, s_list)
    {
        if (((WifiDeviceConfig *)(tmp->user_data))->netId == networkId)
        {
            ((WifiDeviceConfig *)(tmp->user_data))->isHiddenSsid = 1;
            return WIFI_SUCCESS;
        }
    }
    return res;
}

/**
 * @brief Enable a hotspot configuration matching a specified <b>networkId</b>. If the config is enabled, it will
 * be connected automatically when wifi is enabled. When the config is added, it is enabled in default.
 *
 * @param networkId Indicates the <b>networkId</b> matching the hotspot configuration to enable.
 * @return Returns {@link WIFI_SUCCESS} if the hotspot configuration is enabled; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode EnableDeviceConfig(int networkId)
{
    WifiErrorCode res = ERROR_WIFI_INVALID_ARGS;
    // 遍历， 不可以出队
    QUEUE_NODE *tmp;
    LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_deviceConfigQueue].head, QUEUE_NODE, s_list)
    {
        if (((WifiDeviceConfig *)(tmp->user_data))->netId == networkId)
        {
            ((WifiDeviceConfig *)(tmp->user_data))->isHiddenSsid = 0;
            return WIFI_SUCCESS;
        }
    }
    return res;
}

/**
 * @brief Connects to a hotspot matching a specified <b>networkId</b>.
 *
 * Before calling this function, call {@link AddDeviceConfig} to add a hotspot configuration. \n
 *
 * @param networkId Indicates the <b>networkId</b> matching the target hotspot.
 * @return Returns {@link WIFI_SUCCESS} if the hotspot is connected; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode ConnectTo(int networkId)
{
    WifiErrorCode res = ERROR_WIFI_INVALID_ARGS;
    // 遍历， 不可以出队
    QUEUE_NODE *tmp;
    LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_deviceConfigQueue].head, QUEUE_NODE, s_list)
    {
        // 找到对应id的config
        if (((WifiDeviceConfig *)(tmp->user_data))->netId == networkId)
        {
            ConnectToDevice((WifiDeviceConfig *)(tmp->user_data));
            return WIFI_SUCCESS;
        }
    }
    return res;
}

/**
 * @brief Connect to a hotspot by config.
 *
 * @param config is device configuration to connect the Wi-Fi network.
 * @return Returns {@link WIFI_SUCCESS} if the hotspot is connected; returns an error code defined in
 * {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode ConnectToDevice(const WifiDeviceConfig *config)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Connect Wi-Fi ... \n");
    // fflush(stdout);
#endif
    res = SendWifiCommand(STA_JOIN_HOTSPOT, (char *)config);
    if (res == WIFI_SUCCESS)
    {
        res = WaitWifiResponse(STA_JOIN_HOTSPOT, NULL, WIFI_TIMEOUT);
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    // 执行connect回调
    ConnectCB(res);
    return res;
}

/**
 * @brief Disconnects this Wi-Fi connection.
 *
 * @return Returns {@link WIFI_SUCCESS} if this Wi-Fi connection is disconnected; returns an error code defined
 * in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode Disconnect(void)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Disconnect Wi-Fi ... \n");
    // fflush(stdout);
#endif
    res = SendWifiCommand(STA_EXIT_HOTSPOT, NULL);
    if (res == WIFI_SUCCESS)
    {
        res = WaitWifiResponse(STA_EXIT_HOTSPOT, NULL, WIFI_TIMEOUT);
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    // 执行disconnect回调
    DisConnectCB(res);
    return res;
}

/**
 * @brief Obtains information about the connected hotspot.
 *
 * @param result Indicates the information about the connected hotspot.
 * @return Returns {@link WIFI_SUCCESS} if the information about the connected hotspot is obtained; returns an error
 * code defined in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode GetLinkedInfo(WifiLinkedInfo *result)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Get linked info ... \n");
    // fflush(stdout);
#endif
    if (result == NULL){
        return ERROR_WIFI_INVALID_ARGS;
    }
    res = SendWifiCommand(STA_CURRENT_HOTSPOT, NULL);
    if (res == WIFI_SUCCESS){
        res = WaitWifiResponse(STA_CURRENT_HOTSPOT, (char *)result, WIFI_TIMEOUT);
    }
    printf("hehehehehe\n");
    if (res == WIFI_SUCCESS){
        // 获取ip
        IpInfo ipinfo;
        res = GetIpInfo(&ipinfo);
        if (res == WIFI_SUCCESS){
            result->ipAddress = ipinfo.ipAddress;
        }
    }
    result->band = HOTSPOT_BAND_TYPE_2G;
    result->disconnectedReason = 0;
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    return res;
}

/**
 * @brief Obtains the MAC address of this device.
 *
 * @param result Indicates the MAC address of this device. It is a char array whose length is 6.
 * @return Returns {@link WIFI_SUCCESS} if the MAC address of this device is obtained; returns an error code defined
 * in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode GetDeviceMacAddress(unsigned char *result)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Get mac ... \n");
    // fflush(stdout);
#endif
    if (result == NULL)
    {
        return ERROR_WIFI_INVALID_ARGS;
    }
    res = SendWifiCommand(STA_GET_MAC, NULL);
    if (res == WIFI_SUCCESS)
    {
        res = WaitWifiResponse(STA_GET_MAC, (char *)result, WIFI_TIMEOUT);
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    return res;
}


/**
 * @brief Starts a Wi-Fi scan based on a specified parameter.
 *
 * Only results matching the specified parameter will be returned for the Wi-Fi scan.\n
 *
 * @param params Indicates the pointer to the parameter for starting the Wi-Fi scan.
 * For details, see {@link WifiScanParams}.
 * @return Returns {@link WIFI_SUCCESS} if the Wi-Fi scan is started successfully;
 * returns an error code defined in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode AdvanceScan(WifiScanParams *params)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Scan wifi advance ... \n");
    // fflush(stdout);
#endif
    // 不存在，则创建扫描队列
    if (g_scanQueue == 0xff)
    {
        if (QueueCreate(&g_scanQueue, 100, WIFI_SCAN_HOTSPOT_LIMIT))
        {
            printf("%s line %d error : create queue node failed...\n", __func__, __LINE__);
            return ERROR_WIFI_UNKNOWN;
        }
    }
    else
    {
        // 存在，则清空队列
        QueueClear(g_scanQueue);
    }
    res = SendWifiCommand(STA_SCAN_HOTSPOT_CUSTOM, (char *)params);
    if (res == WIFI_SUCCESS)
    {
        res = WaitWifiResponse(STA_SCAN_HOTSPOT_CUSTOM, NULL, WIFI_TIMEOUT);
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    // 从队列g_scanQueue解析数据
    ScanCB(res);
    return res;
}

/*
 * @brief get the ip address.
 *
 * @return Returns {@link WIFI_SUCCESS} if the IP is got; returns an error code defined
 * in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode GetIpInfo(IpInfo *info)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("Get ip ... \n");
    // fflush(stdout);
#endif
    if (info == NULL)
    {
        return ERROR_WIFI_INVALID_ARGS;
    }
    res = SendWifiCommand(STA_GET_IP, NULL);
    if (res == WIFI_SUCCESS)
    {
        res = WaitWifiResponse(STA_GET_IP, (char *)info, WIFI_TIMEOUT);
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    return res;
}

/**
 * @brief Obtains the signal level indicated by a specified received signal strength indicator (RSSI) and frequency
 * band.
 *
 *
 * Based on the signal level, you can display the signal strength represented by the number of signal bars. \n
 *
 * @param rssi Indicates the RSSI.
 * @param band Indicates the frequency band, either {@link HOTSPOT_BAND_TYPE_5G} or {@link HOTSPOT_BAND_TYPE_2G}.
 * @return Returns the signal level if it is obtained; returns <b>-1</b> otherwise.
 * @since 7
 */
int GetSignalLevel(int rssi, int band)
{
    if (band == HOTSPOT_BAND_TYPE_2G)
    {
        if (rssi >= RSSI_LEVEL_4_2_G)
            return RSSI_LEVEL_4;
        if (rssi >= RSSI_LEVEL_3_2_G)
            return RSSI_LEVEL_3;
        if (rssi >= RSSI_LEVEL_2_2_G)
            return RSSI_LEVEL_2;
        if (rssi >= RSSI_LEVEL_1_2_G)
            return RSSI_LEVEL_1;
    }

    if (band == HOTSPOT_BAND_TYPE_5G)
    {
        if (rssi >= RSSI_LEVEL_4_5_G)
            return RSSI_LEVEL_4;
        if (rssi >= RSSI_LEVEL_3_5_G)
            return RSSI_LEVEL_3;
        if (rssi >= RSSI_LEVEL_2_5_G)
            return RSSI_LEVEL_2;
        if (rssi >= RSSI_LEVEL_1_5_G)
            return RSSI_LEVEL_1;
    }
    return RSSI_LEVEL_1;
}

/**
 * @brief Registers a callback for a specified Wi-Fi event.
 *
 * The registered callback will be invoked when the Wi-Fi event defined in {@link WifiEvent} occurs. \n
 *
 * @param event Indicates the event for which the callback is to be registered.
 * @return Returns {@link WIFI_SUCCESS} if the callback is registered successfully; returns an error code defined
 * in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode RegisterWifiEvent(WifiEvent *event)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("RegisterWifiEvent ... \n");
    // fflush(stdout);
#endif
    // 如果不存在，则创建队列
    if (g_wifiEventQueue == 0xff)
    {
        if (QueueCreate(&g_wifiEventQueue, sizeof(WifiEvent), WIFI_MAX_EVENT_SIZE))
        {
            return ERROR_WIFI_UNKNOWN;
        }
    }
    //else
    {
        //  如果小于最大长度，则可插入
        if (g_queueInfo[g_wifiEventQueue].cur_len < WIFI_MAX_EVENT_SIZE)
            QueueIn(g_wifiEventQueue, (char *)event);
        else
            res = ERROR_WIFI_NO_MEMORY;
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    return res;
}

/**
 * @brief Unregisters a callback previously registered for a specified Wi-Fi event.
 *
 * @param event Indicates the event for which the callback is to be unregistered.
 * @return Returns {@link WIFI_SUCCESS} if the callback is unregistered successfully; returns an error code defined
 * in {@link WifiErrorCode} otherwise.
 * @since 7
 */
WifiErrorCode UnRegisterWifiEvent(const WifiEvent *event)
{
    WifiErrorCode res = WIFI_SUCCESS;
#if WIFI_DEBUG
    printf("======================\n");
    printf("UnRegisterWifiEvent ... \n");
    // fflush(stdout);
#endif
    // 遍历， 不可以出队
    QUEUE_NODE *tmp, *node;
    LOS_DL_LIST_FOR_EACH_ENTRY(tmp, &g_queueInfo[g_wifiEventQueue].head, QUEUE_NODE, s_list)
    {
        if (((uint32_t *)(tmp->user_data)) == (uint32_t *)event)
        {
            // 找到前驱
            node = LOS_DL_LIST_ENTRY(tmp->s_list.pstPrev, QUEUE_NODE, s_list);
            // 删除
            LOS_ListDelete(&(tmp->s_list));
            // 释放
            free(tmp->user_data);
            free(tmp);
            tmp = node;
            return WIFI_SUCCESS;
        }
    }
#if WIFI_DEBUG
    PrintWifiError(res);
#endif
    return res;
}